/* Name: Fog
 * Author: John Gideon
 * Date: 5-8-12
 * Description: Blocks the view under from the user and can only be removed by conquering Planets, AsteroidFields, and
 *  Nebulas, as well as by deploying Probes.
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

import java.awt.Color;
import java.awt.Graphics2D;

public class Fog extends Item {
	private static Boolean fog[][];
	private static int numX;
	private static int numY;
	private static Boolean mouseIsInside = false;
	final static int size = 16;
	static Boolean enabled = true;
	
	// Initializes the fog array to block the entire room
	public Fog() {
		Game.create((Item)this, 99999); // Needed for update, draw, and destroy
		colType = COL_NONE; // Needed for collision checking
		numX = (GameScreen.getWidth()/size)+1;
		numY = (GameScreen.getHeight()/size)+1;
		fog = new Boolean[numX][numY];
		for (int x = 0; x < numX; x++) {
			for (int y = 0; y < numY; y++) {
				fog[x][y] = true;
			}
		}
	}
	
	// Called every frame: Draws fog primitive at every position needed to block the view. 
	// Slightly transparent if easy difficulty.
	public void draw() {
		if (enabled) {
			Graphics2D gh = Game.getGraphicsHandle();
			if (Game.difficulty == Game.DIFFICULTY_EASY) {
				gh.setColor(new Color(0.05f,0.05f,0.05f,0.85f));
			} else {
				gh.setColor(new Color(0.05f,0.05f,0.05f));
			}
			int x1 = ViewControl.getX()/size; if (x1 < 0) {x1 = 0;}
			int x2 = ((ViewControl.getX()+Game.screenWidth-1)/size); if (x2 >= numX) {x2 = numX - 1;}
			int y1 = ViewControl.getY()/size; if (y1 < 0) {y1 = 0;}
			int y2 = ((ViewControl.getY()+Game.screenHeight-1)/size); if (y2 >= numY) {y2 = numY - 1;}
			for (int x = x1; x <= x2; x++) {
				for (int y = y1; y <= y2; y++) {
					if (fog[x][y]) {
						int xpos = (x*size);
						int ypos = (y*size);
						gh.fillRect(xpos, ypos, size, size);
					}
				}
			}
		}
	}
	
	// Removes the fog within a certain radius of a point
	public static void uncover(int px, int py, int pr) {
		int x1 = (px-pr)/size;
		if (x1 < 0) {x1 = 0;}
		int x2 = ((px+pr)/size)+1;
		if (x2 >= numX) {x2 = numX-1;}
		int y1 = (py-pr)/size;
		if (y1 < 0) {y1 = 0;}
		int y2 = ((py+pr)/size)+1;
		if (y2 >= numY) {y2 = numY-1;}
		for (int x = x1; x <= x2; x++) {
			for (int y = y1; y <= y2; y++) {
				if (fog[x][y]) {
					int xpos = (x*size);
					int ypos = (y*size);
					if (Math.sqrt(Math.pow(px-xpos,2)+Math.pow(py-ypos,2)) < pr) {
						fog[x][y] = false;
					}
				}
			}
		}
	}
	
	// Called every frame: Updates the status variable determining whether or not the mouse is inside fog
	public void update() {
		if (fog != null && ViewControl.mouseX() != 999999) {
			int x = (ViewControl.mouseX())/size;
			int y = (ViewControl.mouseY())/size;
			mouseIsInside = fog[x][y];
		}
	}
	
	// Returns the status variable or false if fog not enabled (for debugging)
	public static Boolean mouseIsInside() {
		return mouseIsInside && enabled;
	}
	
	// Returns whether or not the fog is uncovered at a certain point
	public static Boolean pointIsInside(int px, int py) {
		int x = px/size;
		int y = py/size;
		return fog[x][y] && enabled;
	}
}
